{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import pandas as pd\n",
    "from keras.preprocessing.text import text_to_word_sequence\n",
    "\n",
    "\n",
    "def get_list_of_dicts(fname): return [json.loads(i) for i in open(fname, \"rt\")]\n",
    "\n",
    "\n",
    "def add_user_reviews(x):\n",
    "    ur = user_reviews.loc[x[\"reviewerID\"]].drop(x[\"asin\"])\n",
    "    mr = movie_reviews.loc[x[\"asin\"]].drop(x[\"reviewerID\"])\n",
    "    x[\"userReviews\"] = ur[\"reviewText\"].tolist()\n",
    "    x[\"movieReviews\"] = mr[\"reviewText\"].tolist()\n",
    "    return x\n",
    "\n",
    "\n",
    "def clean(text):\n",
    "    return text_to_word_sequence(text,\n",
    "                                 filters='!\"#$%&()*+,-./:;<=>?@[\\\\]^_`{|}~\\t\\n',\n",
    "                                 lower=True, split=\" \")\n",
    "\n",
    "\n",
    "def clean_one(x):\n",
    "    return list(map(clean, x[2:-2].split()))\n",
    "\n",
    "\n",
    "def clean_review_text(x):\n",
    "    x[\"reviewText\"] = clean_each(x[\"reviewText\"])\n",
    "    return x\n",
    "\n",
    "\n",
    "raw_data = get_list_of_dicts(\"../data/reviews_Amazon_Instant_Video_5.json\")\n",
    "\n",
    "data = pd.DataFrame(raw_data).loc[:,\n",
    "                                  [\"reviewerID\",\n",
    "                                   \"reviewText\",\n",
    "                                   \"asin\",\n",
    "                                   \"overall\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cleaned_text = data.loc[:, [\"reviewerID\", \"asin\", \"overall\"]]\n",
    "cleaned_text.loc[:, \"reviewText\"] = data.loc[:, \"reviewText\"].apply(clean)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cleaned_text.to_csv(\"../data/cleaned_reviews.csv\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
